using System.Runtime.InteropServices;
using UnityEngine;
namespace NuoYan.Extension
{
    public class InputExtension
    {
        /// <summary>
        /// 鼠标在屏幕上的坐标  ——> 世界坐标
        /// </summary>
        /// <value></value>
        public static Vector3 MousePointToWorldPoint
        {
            get
            {
                return Camera.main.ScreenToWorldPoint(Input.mousePosition);
            }
        }
        /// <summary>
        /// 鼠标下的世界物体
        /// </summary>
        /// <value></value>
        public static GameObject MouseRaycastObject
        {
            get
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    return hit.collider.gameObject;
                }
                return null;
            }
        }
        /// <summary>
        /// 鼠标下的世界物体的坐标 ——> 屏幕坐标
        /// </summary>
        /// <value></value>
        public static Vector3 ObjectPointToScreenPoint
        {
            get
            {
                return Camera.main.WorldToScreenPoint(MouseRaycastObject.transform.position);
            }
        }
        /// <summary>
        /// 判断鼠标是否在屏幕内
        /// </summary>
        /// <value></value>
        public static bool IsMousePointInSceen
        {
            get
            {
                // 获取鼠标在屏幕上的位置
                Vector3 mousePosition = Input.mousePosition;

                // 检查鼠标位置是否在屏幕范围内
                // 注意：屏幕坐标原点(0,0)在左下角，右上角为(Screen.width, Screen.height)
                if (mousePosition.x >= 0 && mousePosition.x <= Screen.width &&
                    mousePosition.y >= 0 && mousePosition.y <= Screen.height)
                {
                    return true;
                }

                return false;
            }
        }
        /// <summary>
        /// 鼠标是否在一个rect内
        /// </summary>
        /// <param name="rectTransform"></param>
        /// <param name="camera"></param>
        /// <param name="needActive">需要Rect物体处于激活？</param>
        /// <returns></returns>
        public static bool IsMousePointInRect(RectTransform rectTransform, Camera camera, bool needActive = true)
        {
            // 检查RectTransform是否有效
            if (rectTransform == null)
                return false;
            if (needActive)
            {
                if (rectTransform.gameObject.activeSelf)
                {
                    // 将鼠标屏幕坐标转换为RectTransform本地坐标系下的点，并判断是否在范围内
                    return RectTransformUtility.RectangleContainsScreenPoint(
                        rectTransform,
                        Input.mousePosition,
                        camera
                    );
                }
                return false;
            }
            else
            {
                // 将鼠标屏幕坐标转换为RectTransform本地坐标系下的点，并判断是否在范围内
                return RectTransformUtility.RectangleContainsScreenPoint(
                    rectTransform,
                    Input.mousePosition,
                    camera
                );
            }
        }
        /// <summary>
        /// 鼠标在rect区域内的局部坐标
        /// </summary>
        /// <param name="input"></param>
        /// <param name="rect"></param>
        /// <param name="camera"></param>
        /// <returns></returns>
        public static Vector2 MousePointToRectPoint(RectTransform rect, Camera camera)
        {
            RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, Input.mousePosition, camera, out Vector2 localPoint);
            return localPoint;
        }
        /// <summary>
        /// 鼠标在屏幕上的坐标  ——> 世界物体相对于transform的局部坐标
        /// </summary>
        /// <param name="transform"></param>
        /// <returns></returns>
        public static Vector3 MousePointToWorldLocalPoint(Transform transform)
        {
            return transform.InverseTransformPoint(MousePointToWorldPoint);
        }

        // 引入Windows系统API
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);
        /// <summary>
        /// 转换Unity屏幕坐标到系统屏幕坐标,使用系统API设置鼠标位置
        /// </summary>
        /// <param name="screenPosition"></param>
        public static void SetMousePosition(Vector2 screenPosition)
        {
            // Unity的屏幕坐标原点在左下角，而Windows系统原点在左上角
            int x = (int)screenPosition.x;
            int y = Screen.height - (int)screenPosition.y;

            // 调用系统API设置鼠标位置
            SetCursorPos(x, y);
        }
    }
}
